const {sequelize} = require('../../core/databases')
const {Sequelize, Model} = require('sequelize')
const bcrypt = require('bcrypt')
const {NotFound, AuthFailed} = require('../../core/http-exception')

class User extends Model {

}

User.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    nickname: Sequelize.STRING,
    email: {
      type: Sequelize.STRING(128),
      unique: true
    },
    password: {
      type: Sequelize.STRING,
      set(val) {
        const salt = bcrypt.genSaltSync(10)
        const pwd = bcrypt.hashSync(val, salt)
        this.setDataValue('password', pwd)
      }
    },
    openid: {
      type: Sequelize.STRING(64),
      unique: true
    }
  },
  {
    sequelize,
    tableName: 'user' // 自定义表格名称
  }
)

module.exports = {User}